package com.pls.helpers;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

import com.plsDTO.dataObjects.Employee;
import com.plsDTO.dataObjects.EmployeeWorkstation;
import com.plsDTO.dataObjects.Project;
import com.plsDTO.dataObjects.Seat;
import com.plsDTO.dataObjects.WorkstationMap;

public class ExportToFile {

	public ExportToFile() {
		// TODO Auto-generated constructor stub
	}

	public static boolean createTextFile(EmployeeWorkstation empWork,
			ArrayList<EmployeeWorkstation> workStationMap) {

		ArrayList<Project> projects = empWork.getProjectList();
		Employee employee = empWork.getEmployee();
		Seat seat = empWork.getSeatDetails();

		File file = new File("bin\\", "Employee.txt");

		// creates the file
		try {
			file.createNewFile();
			// creates a FileWriter Object
			FileWriter writer = new FileWriter(file);
			// Writes the content to the file

			// Write the project
			writer.write(employee.getFirstName() + " " + employee.getLastName()
					+ "\r\n");
			for (int i = 0; i < projects.size(); i++) {

				writer.write(projects.get(i).getProjectName());
				if (i != projects.size() - 1) {
					writer.write(",");
				}
			}
			writer.write("\r\n");
			writer.write(seat.getSeatCode() + "\r\n");
			writer.write(seat.getSeatType() + "\r\n");
			writer.write(seat.getQuadrant() + "\r\n");
			writer.write(seat.getLocalNumber() + "\r\n");
			writer.write("\r\n\r\n");

			// write the seat map
			for (int x = 0; x < 25; x = x + 5) {
				for (int i = x; i < x + 5; i++) {
					writer.write(workStationMap.get(i).getSeatDetails()
							.getSeatCode());
					writer.write("     ");
				}
				writer.write("\r\n");
				for (int i = x; i < x + 5; i++) {
					if (workStationMap.get(i).getEmployee().getEmployeeId() == empWork
							.getEmployee().getEmployeeId()) {
						writer.write("*"
								+ workStationMap.get(i).getEmployee()
										.getEmployeeId() + "*");
						writer.write("       ");
					} else if (workStationMap.get(i).getEmployee()
							.getEmployeeId() != 0) {
						writer.write(workStationMap.get(i).getEmployee()
								.getEmployeeId()
								+ "");
						writer.write("         ");
					} else {
						writer.write("NA    ");
						writer.write("         ");
					}

				}

				writer.write("\r\n");
				for (int i = x; i < x + 5; i++) {

					if (workStationMap.get(i).getSeatDetails().getLocalNumber() != 0) {
						writer.write(workStationMap.get(i).getSeatDetails()
								.getLocalNumber()
								+ "");
						writer.write("          ");
					} else {
						writer.write("--   ");
						writer.write("          ");
					}
				}
				writer.write("\r\n\r\n");
			}

			writer.flush();
			writer.close();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			return false;
		}
		return true;

	}

	public static void createExcelFile(EmployeeWorkstation empWork,
			ArrayList<EmployeeWorkstation> workStationMap) {

		ArrayList<Project> projects = empWork.getProjectList();
		Employee employee = empWork.getEmployee();
		Seat seat = empWork.getSeatDetails();

		HSSFWorkbook workbook = new HSSFWorkbook();
		HSSFSheet sheet = workbook.createSheet("Sample sheet");

		int rownum = 0;
		for (int x = 0; x < 25; x = x + 5) {
			Row row = sheet.createRow(rownum++);
			int cellnum = 0;
			for (int i = x; i < x + 5; i++) {
				Cell cell = row.createCell(cellnum++);

				if (workStationMap.get(i).getEmployee().getEmployeeId() == empWork
						.getEmployee().getEmployeeId()) {
					cell.setCellValue(workStationMap.get(i).getSeatDetails()
							.getSeatCode()
							+ "   "
							+ workStationMap.get(i).getEmployee()
									.getEmployeeId());
				} else if (workStationMap.get(i).getEmployee().getEmployeeId() != 0) {
					cell.setCellValue(workStationMap.get(i).getSeatDetails()
							.getSeatCode()
							+ "   "
							+ "*"
							+ workStationMap.get(i).getEmployee()
									.getEmployeeId() + "*");
				} else {
					cell.setCellValue(workStationMap.get(i).getSeatDetails()
							.getSeatCode() + " Unallocated Seat");
				}
			}

		}

		try {
			FileOutputStream out = new FileOutputStream(
					new File("bin\\new.xls"));
			workbook.write(out);
			out.close();
			System.out.println("\n\nExcel written successfully..\n\n");

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}
